<!-- src/views/Register.vue -->
<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { authApi } from '../api/auth'
import { ElMessage, ElForm, ElFormItem, ElInput, ElButton, ElCard, ElSelect, ElOption } from 'element-plus'

const router = useRouter()

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  email: '',
  role: ''
})

const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, message: '用户名长度至少3位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择角色', trigger: 'change' }
  ]
})

const registerFormRef = ref()
const loading = ref(false)

const submitForm = async () => {
  if (!registerFormRef.value) return

  registerFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const { confirmPassword, ...registerData } = registerForm
        const response = await authApi.register(registerData)

        ElMessage.success('注册成功，请登录')
        router.push('/login')
      } catch (error) {
        ElMessage.error(error.response?.data?.message || '注册失败')
      } finally {
        loading.value = false
      }
    } else {
      ElMessage.error('请填写完整的注册信息')
      return false
    }
  })
}

const backToLogin = () => {
  router.push('/login')
}
</script>

<template>
  <div class="register-container">
    <div class="register-background">
      <div class="background-shapes">
        <div class="shape shape-1"></div>
        <div class="shape shape-2"></div>
        <div class="shape shape-3"></div>
      </div>
    </div>

    <div class="register-content">
      <!-- 左侧信息区 -->
      <div class="info-section">
        <h1>大学生专业实习管理系统</h1>
        <h2>欢迎注册</h2>
        <p>连接学生、教师与企业的一站式实习管理平台</p>
        <div class="features">
          <div class="feature">实习过程管理</div>
          <div class="feature">多方协同工作</div>
          <div class="feature">在线报告提交</div>
        </div>
      </div>

      <!-- 右侧注册区 -->
      <div class="form-section">
        <el-card class="register-card">
          <h3>用户注册</h3>
          <p class="form-subtitle">请填写您的注册信息</p>

          <el-form
              :model="registerForm"
              :rules="rules"
              ref="registerFormRef"
              @submit.native.prevent="submitForm"
          >
            <el-form-item prop="username">
              <el-input
                  v-model="registerForm.username"
                  placeholder="请输入用户名"
                  prefix-icon="User"
              />
            </el-form-item>

            <el-form-item prop="email">
              <el-input
                  v-model="registerForm.email"
                  placeholder="请输入邮箱"
                  prefix-icon="Message"
              />
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                  v-model="registerForm.password"
                  type="password"
                  placeholder="请输入密码"
                  prefix-icon="Lock"
                  show-password
              />
            </el-form-item>

            <el-form-item prop="confirmPassword">
              <el-input
                  v-model="registerForm.confirmPassword"
                  type="password"
                  placeholder="请确认密码"
                  prefix-icon="Lock"
                  show-password
              />
            </el-form-item>

            <el-form-item prop="role">
              <el-select
                  v-model="registerForm.role"
                  placeholder="请选择角色"
                  style="width: 100%"
              >
                <el-option label="学生" value="STUDENT"></el-option>
                <el-option label="教师" value="TEACHER"></el-option>
                <el-option label="企业" value="COMPANY"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item>
              <el-button
                type="primary"
                native-type="submit"
                class="register-btn"
                :loading="loading"
              >
                注册
              </el-button>
            </el-form-item>
          </el-form>

          <div class="form-footer">
            <span>已有账号？<a @click="backToLogin">立即登录</a></span>
          </div>
        </el-card>

        <div class="copyright">
          专业实习管理平台 ©2025
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.register-container {
  width: 100vw;
  height: 100vh;
  margin: 0;
  padding: 0;
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
}

.register-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.background-shapes {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.shape {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.shape-1 {
  width: 200px;
  height: 200px;
  top: -25px;
  right: -25px;
}

.shape-2 {
  width: 150px;
  height: 150px;
  bottom: 100px;
  left: -50px;
}

.shape-3 {
  width: 100px;
  height: 100px;
  bottom: -30px;
  right: 30%;
}

.register-content {
  position: relative;
  display: flex;
  width: 100%;
  height: 100%;
  max-width: 1200px;
  margin: 0 auto;
  align-items: center;
  padding: 0 40px;
  z-index: 1;
}

@media (max-width: 768px) {
  .info-section{
    display: none;
  }
  .form-section{
    flex: 0 0 auto;
  }
  .register-content{
    justify-content: center;
  }
}

.info-section {
  flex: 1;
  color: white;
  max-width: 500px;
}

.info-section h1 {
  font-size: 2.5rem;
  font-weight: 700;
  margin-bottom: 1rem;
  line-height: 1.2;
}

.info-section h2 {
  font-size: 1.8rem;
  font-weight: 400;
  margin-bottom: 1.5rem;
  opacity: 0.9;
}

.info-section p {
  font-size: 1.1rem;
  margin-bottom: 2.5rem;
  opacity: 0.8;
  line-height: 1.6;
}

.features {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.feature {
  padding: 12px 20px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  font-size: 1rem;
}

.form-section {
  flex: 0 0 400px;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: transparent;
}

.register-card {
  width: 100%;
  padding: 40px 30px;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
}

.register-card h3 {
  text-align: center;
  color: #2c3e50;
  font-size: 1.5rem;
  margin-bottom: 8px;
  font-weight: 600;
}

.form-subtitle {
  text-align: center;
  color: #7f8c8d;
  margin-bottom: 30px;
  font-size: 0.9rem;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  height: 48px;
}

:deep(.el-select__wrapper) {
  border-radius: 8px;
  height: 48px;
}

.register-btn {
  width: 100%;
  height: 48px;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 500;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
}

.register-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.form-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
  font-size: 0.9rem;
  color: #7f8c8d;
}

.form-footer a {
  color: #667eea;
  cursor: pointer;
  text-decoration: none;
}

.copyright {
  margin-top: 30px;
  color: rgba(255, 255, 255, 0.8);
  font-size: 0.9rem;
  text-align: center;
}
</style>
